package com.example.administrator.yanfoxconn.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.activity.TimeDatePickerDialog;
import com.example.administrator.yanfoxconn.bean.LCItems;
import com.example.administrator.yanfoxconn.bean.LCMessage;
import com.example.administrator.yanfoxconn.bean.SelectModel;
import com.example.administrator.yanfoxconn.bean.ZhiyinshuiCheckMsg;
import com.example.administrator.yanfoxconn.intent.PhotoPickerIntent;
import com.example.administrator.yanfoxconn.intent.PhotoPreviewIntent;
import com.example.administrator.yanfoxconn.utils.ToastUtils;
import com.example.administrator.yanfoxconn.widget.MyGridView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import static androidx.core.app.ActivityCompat.startActivityForResult;

/**
 * Created by S1007989 on 2020/7/26
 * description：點檢項目點檢共用
 */
public class LCMainUpCheckAdapter extends RecyclerView.Adapter<LCMainUpCheckAdapter.ViewHolder> {
    private List<LCMessage> lists;
    private List<LCItems>infoList;
    private static HashMap<Integer, String> isSelected; //存放checkBox状态的map
    private static HashMap<Integer, String> etMap = new HashMap<>();//存放editText值的map

    public static Context mContext;
    private static int pos;
    public boolean isChange;

    public String flag="";
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvContent;// 檢查項目
        CheckBox cbCheck;//單選
        EditText etAbnormal;// 填寫信息
        LinearLayout llAbnormal;//

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cbCheck = itemView.findViewById(R.id.cb_check);
            tvContent = itemView.findViewById(R.id.tv_content);
            llAbnormal = itemView.findViewById(R.id.ll_abnormal);
            etAbnormal = itemView.findViewById(R.id.et_abnormal);
        }

    }
    // 初始化isSelected的数据
    private void initDate() {
        for (int i = 0; i < lists.size(); i++) {
            isSelected.put(i, "true");
        }
    }
    public LCMainUpCheckAdapter(Context context, List<LCMessage> lists, HashMap<Integer, String> isSelected){
        this.lists=lists;
        this.isSelected=isSelected;
        this.mContext = context;
        initDate();
    }
    public LCMainUpCheckAdapter(Context context, List<LCMessage> lists, HashMap<Integer, String> isSelected, List<LCItems> infoLists){
        this.lists=lists;
        this.isSelected=isSelected;
        this.mContext = context;
        this.infoList = infoLists;
        this.flag="info";
        initDate();
    }

    /**
     * 重写 避免滑动过程界面混乱
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        return position;
    }
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_lc_check,parent,false);
        ViewHolder holder = new ViewHolder(view);

        //異常輸入框
        holder.etAbnormal.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                holder.etAbnormal.setFocusable(true);
                holder.etAbnormal.setFocusableInTouchMode(true);
                holder.etAbnormal.requestFocus();
            }
        });
        //異常輸入框
        holder.etAbnormal.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                int position = holder.getAdapterPosition();
                etMap.put(position,holder.etAbnormal.getText().toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        holder.cbCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                //显示checkBox
//                holder.cbCheck.setChecked(lists.get(position).getBo());
                if (isChange) {
                    if (isSelected.get(position).equals("true")) {
                        holder.llAbnormal.setVisibility(View.VISIBLE);
                        isSelected.put(position, "false");
                        setIsSelected(isSelected);

                    } else {
                        holder.llAbnormal.setVisibility(View.GONE);
//                        holder.etAbnormal.setVisibility(View.GONE);
                        holder.etAbnormal.setText("");
                        etMap.put(position,holder.etAbnormal.getText().toString());
                        isSelected.put(position, "true");
                        setIsSelected(isSelected);

                    }
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        int num=position+1;
        LCMessage lcMessage = lists.get(position);
        if (lcMessage.getFlag().equals("ZD")){
            holder.tvContent.setText(lcMessage.getPoint_name());
        }
        if (!flag.equals("")){
            for (int i=0;i<infoList.size();i++){
                if (infoList.get(i).getType().equals("ZD")&&infoList.get(i).getKeypoint().equals(lcMessage.getPoint_name())){
                    holder.etAbnormal.setText(infoList.get(i).getRecord());
                    holder.cbCheck.setChecked(true);
                    if (isSelected.get(position).equals("true")) {
                        holder.llAbnormal.setVisibility(View.VISIBLE);
                        isSelected.put(position, "false");
                        setIsSelected(isSelected);
                    }
                }
            }
        }
        isChange = true;
    }

    @Override
    public int getItemCount() {
        Log.e("--------","lists.size()==="+lists.size());
        return lists.size();
    }

    public HashMap<Integer, String> getIsSelected() {
        return isSelected;
    }

    public  void setIsSelected(HashMap<Integer, String> isSelected) {
        this.isSelected = isSelected;
    }

    public static int getPosition() {
        return pos;
    }

    public static void setPosition(int position) {
        LCMainUpCheckAdapter.pos = position;
    }

    public static HashMap<Integer, String> getEtMap() {
        return etMap;
    }

    public static void setEtMap(HashMap<Integer, String> etMap) {
        LCMainUpCheckAdapter.etMap = etMap;
    }

//    public static HashMap<Integer, String> getEtCheckMap() {
//        return etCheckMap;
//    }

//    public static void setEtCheckMap(HashMap<Integer, String> etCheckMap) {
//        LCMainUpCheckAdapter.etCheckMap = etCheckMap;
//    }


}
